package com.lill.org.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lill.common.component.util.IdUtil;
import com.lill.org.constant.ArchitectureType;
import com.lill.org.dao.ICompanyArchitectureDao;
import com.lill.org.dao.IDepartmentDao;
import com.lill.org.model.CompanyArchitectureModel;
import com.lill.org.model.DepartmentModel;
import com.lill.org.service.IDepartmentService;

/**
 * 部门管理Service实现类
 *
 * @author lill
 * @date 2019-04-24 15:25:17
 */
@Service("com.lill.org.service.impl.DepartmentServiceImpl")
public class DepartmentServiceImpl implements IDepartmentService {
	
    @Autowired
    private IDepartmentDao departmentDao;
    @Autowired
    private ICompanyArchitectureDao architectureDao;

    public DepartmentModel queryObject(String id) {
        return departmentDao.queryObject(id);
    }

    public List<DepartmentModel> queryList(Map<String, Object> map) {
        return departmentDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return departmentDao.queryTotal(map);
    }

    @Transactional
    public int save(DepartmentModel orgDepartment) {
    	String id=IdUtil.createIdbyUUID();
    	orgDepartment.setId(id);
    	
    	String parentId=orgDepartment.getParentId();
    	CompanyArchitectureModel parentArchitecture=architectureDao.queryObject(parentId);
    	
    	CompanyArchitectureModel architecture=new CompanyArchitectureModel();
    	architecture.setId(id);
    	architecture.setParentId(parentArchitecture.getId());
    	if(parentArchitecture.getParentList().contains(parentArchitecture.getId())){
    		architecture.setParentList(parentArchitecture.getParentList());
    	}else{
    		architecture.setParentList(parentArchitecture.getParentList()+","+parentArchitecture.getId());
    	}
    	architecture.setType(ArchitectureType.DEPART.getVal());
    	architecture.setNum(0);
    	architectureDao.save(architecture);
        
        return departmentDao.save(orgDepartment);
    }

    @Transactional
    public int update(DepartmentModel orgDepartment) {
    	CompanyArchitectureModel oldArchitecture=architectureDao.queryObject(orgDepartment.getId());
    	if(!oldArchitecture.getParentId().equals(orgDepartment.getParentId())){
    		CompanyArchitectureModel parentArchitecture=architectureDao.queryObject(orgDepartment.getParentId());
    		
    		CompanyArchitectureModel updateArchitecture=new CompanyArchitectureModel();
    		updateArchitecture.setId(orgDepartment.getId());
    		updateArchitecture.setParentId(orgDepartment.getParentId());
    		if(parentArchitecture.getParentList().contains(parentArchitecture.getId())){
    			updateArchitecture.setParentList(parentArchitecture.getParentList());
    		}else{
    			updateArchitecture.setParentList(parentArchitecture.getParentList()+","+parentArchitecture.getId());
    		}
    		architectureDao.update(updateArchitecture);
    		
    		String oldParents=oldArchitecture.getParentList();
    		if(StringUtils.isNotEmpty(oldParents)){
    			String[] oldParentList=oldParents.split(",");
    			Map<String, Object> oldDelMap=new HashMap<String, Object>();
    			oldDelMap.put("id", orgDepartment.getId());
    			for (String oldParentId : oldParentList) {
					/*********利用replace 将子集的父级合替换为空**********/
    				oldDelMap.put("parentOldId", oldParentId);
    				architectureDao.updateDeleteOldParent(oldDelMap);
				}
    		}
    		String newParents=updateArchitecture.getParentList();
    		if(StringUtils.isNotEmpty(newParents)){
    			/*********利用CONCAT 将子集的父级合连接父级**********/
    			Map<String, Object> newAddMap=new HashMap<String, Object>();
    			newAddMap.put("id", orgDepartment.getId());
    			newAddMap.put("parentNewIds", newParents+",");
    			architectureDao.updateAddOldParent(newAddMap);
    		}
    		
//    		Map<String, Object> updateChildMap=new HashMap<String, Object>();
//    		if(updateArchitecture.getParentList().contains(updateArchitecture.getId())){
//    			updateChildMap.put("udapteParents", updateArchitecture.getParentList());
//    		}else{
//    			updateChildMap.put("udapteParents", updateArchitecture.getParentList()+","+updateArchitecture.getId());
//    		}
//    		if(oldArchitecture.getParentList().contains(oldArchitecture.getId())){
//    			updateChildMap.put("oldParents", oldArchitecture.getParentList());
//    		}else{
//    			updateChildMap.put("oldParents", oldArchitecture.getParentList()+","+oldArchitecture.getId());
//    		}
//    		architectureDao.updateChildParentList(updateChildMap);
    		
    		Map<String, Object> mapParam=new HashMap<String, Object>();
    		mapParam.put("ids", oldArchitecture.getParentList().split(","));
    		mapParam.put("num", -1*oldArchitecture.getNum());
    		architectureDao.updatePersonNum(mapParam);
    		
    		mapParam.put("ids", updateArchitecture.getParentList().split(","));
    		mapParam.put("num", oldArchitecture.getNum());
    		architectureDao.updatePersonNum(mapParam);
    	}
        return departmentDao.update(orgDepartment);
    }

    public int delete(String id) {
        return departmentDao.delete(id);
    }

    public int deleteBatch(String[] ids) {
        return departmentDao.deleteBatch(ids);
    }
    
	public int deleteLogicByMap(Map<String, Object> map) {
		return departmentDao.deleteLogicByMap(map);
	}
	
	public DepartmentModel selectId(Map<String, Object> map){
		return departmentDao.selectId(map);
	}

	public String queryFirstRegion(String id) {
		return departmentDao.queryFirstRegion(id);
	}
}
